home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / NCSA_Telnet / PC / msdos / contributions / VGA_bitplanes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-31  |  4.3 KB  |  216 lines

  1. /*****************************************************************************/
  2. /*                              BITPLANE.C                                   */
  3. /*****************************************************************************/
  4. #include <dos.h>
  5. #include <stdlib.h>
  6. #include <mem.h>
  7. #define BYTE unsigned char
  8. #define PLANESIZE 38400
  9. BYTE cur_plane = 0;
  10.  
  11. void setgraphicsmode(int mode)
  12. {
  13.   union REGS regs;
  14.  
  15.   regs.x.ax = mode % 256;
  16.   int86(0x10,®s,®s);
  17. }
  18.  
  19. void setpaletteregister(int registr,int value)
  20. {
  21.   union REGS regs;
  22.  
  23.   regs.x.ax = 0x1000;
  24.   regs.x.bx = ((value % 64) << 8) + (registr % 16);
  25.   int86(0x10,®s,®s);
  26. }
  27.  
  28. void setcolorregister(int registr,int red,int green,int blue)
  29. {
  30.   union REGS regs;
  31.  
  32.   regs.x.ax = 0x1010;
  33.   regs.x.bx = registr;
  34.   regs.x.dx = ((red % 64) << 8) + (regs.x.dx & 0xFF);
  35.   regs.x.cx = ((green % 64) << 8) + blue;
  36.   int86(0x10,®s,®s);
  37. }
  38.  
  39. void setupbitplane()
  40. {
  41.   int c4 = 63;    /* Full brightness    63    */
  42.   int c3 = 54;    /* 3/4  brightness    55    */
  43.   int c2 = 47;    /* Half brightness    48    */
  44.   int c1 = 40;    /* 1/4  brightness    40    */
  45.   int c0 =  0;    /* No   brightness    0    */
  46.   int i;
  47.  
  48.   setgraphicsmode(0x12);
  49.   for (i=0;i<16;i++)
  50.     setpaletteregister(i,i);
  51.   setcolorregister(0,0,c0,c0);    /* For 0000    */
  52.   setcolorregister(1,0,c1,c1);    /* For 0001    */
  53.   setcolorregister(2,0,c1,c1);    /* For 0010    */
  54.   setcolorregister(4,0,c1,c1);    /* For 0100    */
  55.   setcolorregister(8,0,c1,c1);    /* For 1000    */
  56.   setcolorregister(3,0,c2,c2);    /* For 0011    */
  57.   setcolorregister(5,0,c2,c2);    /* For 0101    */
  58.   setcolorregister(6,0,c2,c2);    /* For 0110    */
  59.   setcolorregister(9,0,c2,c2);    /* For 1001    */
  60.   setcolorregister(10,0,c2,c2);    /* For 1010    */
  61.   setcolorregister(12,0,c2,c2);    /* For 1100    */
  62.   setcolorregister(11,0,c3,c3);    /* For 0111    */
  63.   setcolorregister(7,0,c3,c3);    /* For 1011    */
  64.   setcolorregister(13,0,c3,c3);    /* For 1101    */
  65.   setcolorregister(14,0,c3,c3);    /* For 1110    */
  66.   setcolorregister(15,0,c4,c4);    /* For 1111    */
  67. }
  68.  
  69. void plotdot(int x,int y)
  70. {
  71.   unsigned offset;
  72.   BYTE plane,planemask,oldval;
  73.  
  74.   plane = cur_plane;
  75.   /******** Select appropriate memory plane *******/
  76.   planemask = 1 << plane;
  77.   outportb(0x03CE,4);
  78.   outportb(0x03CF,plane);    /* For reading    */
  79.   outportb(0x03C4,2);
  80.   outportb(0x03C5,planemask);    /* For writing    */
  81.   /******* Set value in memory *******/
  82.   offset = y * 80 + x / 8;
  83.   oldval = peekb(0xA000,offset);
  84.   pokeb(0xA000,offset,oldval | (0x80 >> (x % 8)));
  85. }
  86.  
  87. void clearplane()
  88. {
  89.   BYTE plane,planemask,*video_ram;
  90.  
  91.   video_ram = MK_FP(0xA000,0x0000);
  92.   plane = cur_plane;
  93.   /******** Select appropriate memory plane *******/
  94.   planemask = 1 << plane;
  95.   outportb(0x03C4,2);
  96.   outportb(0x03C5,planemask);    /* For writing    */
  97.   memset(video_ram,0,PLANESIZE);
  98. }
  99.  
  100.  
  101. void setplane(BYTE plane)
  102. {
  103.   cur_plane = plane % 4;
  104. }
  105.  
  106. int sign(int x)
  107. {
  108.   (x<0) ? (x=-1) : (x=1);
  109.   return(x);
  110. }
  111.  
  112. int _abs(int x)
  113. {
  114.   if (x<0) x *=-1;
  115.   return(x);
  116. }
  117.  
  118. void swap(int *a,int *b)
  119. {
  120.   int c;
  121.  
  122.   c=*b;
  123.   *b=*a;
  124.   *a=c;
  125. }
  126.  
  127. void _line(int x,int y,int x2,int y2)
  128. {
  129.   int i,steps,sx,sy,dx,dy,e,steep;
  130.  
  131.   dx = _abs(x2-x);
  132.   sx = sign(x2-x);
  133.   dy = _abs(y2-y);
  134.   sy = sign(y2-y);
  135.   steep = dy > dx;
  136.   if (steep) {
  137.     swap(&x,&y);
  138.     swap(&dy,&dx);
  139.     swap(&sx,&sy);
  140.   }
  141.   e = 2*dy-dx;
  142.   for (i=0;i<dx;i++) {
  143.     if (steep) plotdot(y,x);
  144.     else plotdot(x,y);
  145.     while (e > 0) {
  146.       y += sy;
  147.       e -= 2*dx;
  148.     }
  149.     x += sx;
  150.     e += 2*dy;
  151.   }
  152.   plotdot(x2,y2);
  153. }
  154.  
  155. void _circle(int xc,int yc,int radius)
  156. {
  157.   int x,y,d;
  158.  
  159.   x = 0;
  160.   y = radius;
  161.   d = 2 * (1-radius);
  162.   while (y>x) {
  163.     plotdot(xc+x,yc+y);
  164.     plotdot(xc-x,yc+y);
  165.     plotdot(xc+x,yc-y);
  166.     plotdot(xc-x,yc-y);
  167.     plotdot(xc,yc+y);
  168.     plotdot(xc,yc-y);
  169.     plotdot(xc+x,yc);
  170.     plotdot(xc-x,yc);
  171.     if (d+y>0) {
  172.       y--;
  173.       d = d - 2*y +1;
  174.     }
  175.     if (x>d) {
  176.       x++;
  177.       d = d + 2*x + 1;
  178.     }
  179.   }
  180. }
  181.  
  182. main()
  183. {
  184.   int i;
  185.  
  186.   setupbitplane();
  187.   setplane(0);
  188.   for (i=1;i<20;i++)
  189.     _line(100,100,400,i*20);
  190.   getch();
  191.   setplane(1);
  192.   for (i=1;i<20;i++)
  193.     _line(120,120,400,i*20);
  194.   getch();
  195.   setplane(2);
  196.   for (i=1;i<20;i++)
  197.     _line(130,130,400,i*20);
  198.   getch();
  199.   setplane(3);
  200.   for (i=1;i<20;i++)
  201.     _line(140,140,400,i*20);
  202.   getch();
  203.   setplane(0);
  204.   clearplane();
  205.   getch();
  206.   setplane(1);
  207.   clearplane();
  208.   getch();
  209.   setplane(2);
  210.   clearplane();
  211.   getch();
  212.   setplane(3);
  213.   clearplane();
  214.   getch();
  215.   setgraphicsmode(0x3);
  216. }